using System;
using System.Drawing;
using System.Windows.Forms;
using HuaRongDaoGame.Models;
using HuaRongDaoGame.Services;

namespace HuaRongDaoGame.Forms
{
    /// <summary>
    /// 主界面窗体
    /// </summary>
    public partial class MainForm : Form
    {
        private GameState gameState;
        private GameDataService.GameSettings settings;

        public MainForm()
        {
            InitializeGame();
        }

        /// <summary>
        /// 初始化游戏
        /// </summary>
        private void InitializeGame()
        {
            // 加载设置
            settings = GameDataService.LoadSettings();
            
            // 创建游戏状态
            gameState = new GameState();
            gameState.MaxHints = settings.MaxHints;
            
            // 加载统计数据
            gameState.Statistics = GameDataService.LoadStatistics();
            
            // 设置窗体属性
            this.Text = "华容道游戏";
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Size = new Size(600, 500);
            
            // 设置背景色
            this.BackColor = Color.LightGray;
            
            // 创建界面控件
            CreateControls();
        }

        /// <summary>
        /// 创建界面控件
        /// </summary>
        private void CreateControls()
        {
            // 标题标签
            var titleLabel = new Label
            {
                Text = "华容道游戏",
                Font = new Font("微软雅黑", 24, FontStyle.Bold),
                ForeColor = Color.DarkBlue,
                TextAlign = ContentAlignment.MiddleCenter,
                Size = new Size(400, 50),
                Location = new Point(100, 50)
            };

            // 副标题
            var subtitleLabel = new Label
            {
                Text = "HuaRongDao Game",
                Font = new Font("Segoe UI", 12, FontStyle.Italic),
                ForeColor = Color.DarkGray,
                TextAlign = ContentAlignment.MiddleCenter,
                Size = new Size(400, 30),
                Location = new Point(100, 100)
            };

            // 开始新游戏按钮
            var newGameButton = new Button
            {
                Text = "开始新游戏",
                Font = new Font("微软雅黑", 12, FontStyle.Regular),
                Size = new Size(200, 40),
                Location = new Point(200, 160),
                BackColor = Color.LightBlue,
                FlatStyle = FlatStyle.Flat
            };
            newGameButton.Click += NewGameButton_Click;

            // 加载游戏按钮
            var loadGameButton = new Button
            {
                Text = "加载游戏",
                Font = new Font("微软雅黑", 12, FontStyle.Regular),
                Size = new Size(200, 40),
                Location = new Point(200, 220),
                BackColor = Color.LightGreen,
                FlatStyle = FlatStyle.Flat
            };
            loadGameButton.Click += LoadGameButton_Click;

            // 设置按钮
            var settingsButton = new Button
            {
                Text = "设置",
                Font = new Font("微软雅黑", 12, FontStyle.Regular),
                Size = new Size(200, 40),
                Location = new Point(200, 280),
                BackColor = Color.LightYellow,
                FlatStyle = FlatStyle.Flat
            };
            settingsButton.Click += SettingsButton_Click;

            // 帮助按钮
            var helpButton = new Button
            {
                Text = "帮助",
                Font = new Font("微软雅黑", 12, FontStyle.Regular),
                Size = new Size(200, 40),
                Location = new Point(200, 340),
                BackColor = Color.LightCoral,
                FlatStyle = FlatStyle.Flat
            };
            helpButton.Click += HelpButton_Click;

            // 统计按钮
            var statisticsButton = new Button
            {
                Text = "统计",
                Font = new Font("微软雅黑", 12, FontStyle.Regular),
                Size = new Size(200, 40),
                Location = new Point(200, 400),
                BackColor = Color.LightSteelBlue,
                FlatStyle = FlatStyle.Flat
            };
            statisticsButton.Click += StatisticsButton_Click;

            // 退出按钮
            var exitButton = new Button
            {
                Text = "退出",
                Font = new Font("微软雅黑", 12, FontStyle.Regular),
                Size = new Size(200, 40),
                Location = new Point(200, 460),
                BackColor = Color.LightPink,
                FlatStyle = FlatStyle.Flat
            };
            exitButton.Click += ExitButton_Click;

            // 添加控件到窗体
            this.Controls.AddRange(new Control[]
            {
                titleLabel,
                subtitleLabel,
                newGameButton,
                loadGameButton,
                settingsButton,
                helpButton,
                statisticsButton,
                exitButton
            });
        }

        /// <summary>
        /// 开始新游戏按钮点击事件
        /// </summary>
        private void NewGameButton_Click(object sender, EventArgs e)
        {
            var layoutSelectionForm = new LayoutSelectionForm();
            if (layoutSelectionForm.ShowDialog() == DialogResult.OK)
            {
                var selectedLayout = layoutSelectionForm.SelectedLayout;
                if (selectedLayout != null)
                {
                    // 开始新游戏
                    gameState.StartNewGame(selectedLayout);
                    
                    // 打开游戏界面
                    var gameForm = new GameForm(gameState);
                    this.Hide();
                    gameForm.Show();
                    
                    // 监听游戏窗体关闭事件
                    gameForm.FormClosed += (s, args) => 
                    {
                        this.Show();
                    };
                    
                    // 保存统计数据
                    GameDataService.SaveStatistics(gameState.Statistics);
                }
            }
        }

        /// <summary>
        /// 加载游戏按钮点击事件
        /// </summary>
        private void LoadGameButton_Click(object sender, EventArgs e)
        {
            var saveSelectionForm = new SaveSelectionForm();
            if (saveSelectionForm.ShowDialog() == DialogResult.OK)
            {
                var selectedSave = saveSelectionForm.SelectedSave;
                if (!string.IsNullOrEmpty(selectedSave))
                {
                    // 加载游戏
                    var loadedGameState = GameDataService.LoadGame(selectedSave);
                    if (loadedGameState != null)
                    {
                        // 打开游戏界面
                        var gameForm = new GameForm(loadedGameState);
                        this.Hide();
                        gameForm.Show();
                        
                        // 监听游戏窗体关闭事件
                        gameForm.FormClosed += (s, args) => 
                        {
                            this.Show();
                        };
                        
                        // 保存统计数据
                        GameDataService.SaveStatistics(loadedGameState.Statistics);
                    }
                }
            }
        }

        /// <summary>
        /// 设置按钮点击事件
        /// </summary>
        private void SettingsButton_Click(object sender, EventArgs e)
        {
            var settingsForm = new SettingsForm(settings);
            if (settingsForm.ShowDialog() == DialogResult.OK)
            {
                // 更新设置
                settings = settingsForm.Settings;
                gameState.MaxHints = settings.MaxHints;
                
                // 保存设置
                GameDataService.SaveSettings(settings);
            }
        }

        /// <summary>
        /// 帮助按钮点击事件
        /// </summary>
        private void HelpButton_Click(object sender, EventArgs e)
        {
            var helpForm = new HelpForm();
            helpForm.ShowDialog();
        }

        /// <summary>
        /// 统计按钮点击事件
        /// </summary>
        private void StatisticsButton_Click(object sender, EventArgs e)
        {
            var statisticsForm = new StatisticsForm(gameState.Statistics);
            statisticsForm.ShowDialog();
        }

        /// <summary>
        /// 退出按钮点击事件
        /// </summary>
        private void ExitButton_Click(object sender, EventArgs e)
        {
            // 保存统计数据
            GameDataService.SaveStatistics(gameState.Statistics);
            
            // 退出应用程序
            Application.Exit();
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            // 保存统计数据
            GameDataService.SaveStatistics(gameState.Statistics);
            base.OnFormClosing(e);
        }
    }
} 